#include <QPainter>
#include "statebar.h"

StateBar::StateBar(QWidget *parent) :
    QWidget(parent)
{
    this->wifiGrade = 4;
    this->batGrade = 4;
    batGradeCount = 0;
    my_chargeEnable = false;
    my_electProg = 0;
    my_electPerc = 0;
    my_chargeCount = 0;
    my_twink = false;

}

void StateBar::updateElec(QList<int> list)
{
    my_electPerc = list[0];
    my_electProg = list[1];

    if(list[2]  == 1 )
    {
        my_chargingEnable = true;
    }else{
        my_chargingEnable = false;
    }

    if( list[3]  == 1)
    {
       my_alarmEnable = true;
    }else{
       my_alarmEnable = false;
    }

    if( list[4]  == 1)
    {
       my_twinkleEnable = true;
    }else{
       my_twinkleEnable = false;
    }


    if( list[5]  == 1)
    {
       my_onlyPowerEnable = true;
    }else{
       my_onlyPowerEnable = false;
    }
}

void StateBar::updateData(int wGrade, int bGrade)
{
    this->wifiGrade = wGrade;
    this->batGrade = bGrade;
    my_chargeEle = bGrade;
}
void StateBar::updateDateTime(QDateTime dt)
{
    this->dateTime = dt;
}

void StateBar::setChargeEnable(bool enable)
{
    my_chargeEnable = enable;
}

void StateBar::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);
    QRectF rectBorder(0,0,this->width(),this->height());
    //painter.setPen(QPen(Qt::darkGray,5,Qt::SolidLine,Qt::RoundCap,Qt::BevelJoin));
    //painter.drawRoundedRect(rectBorder,5,5);
    //painter.fillRect(rectBorder,QBrush(QColor(1,92,101,220)));
    QPen pen = painter.pen();

    #ifdef WIFI_SIGNAL
    pen.setColor(Qt::gray);
    pen.setWidth(4);
    painter.setPen(pen);
    painter.setBrush(QBrush(Qt::gray));

    painter.drawArc(QRectF(5,8,60,50),50*16,80*16);
    painter.drawArc(QRectF(5+8,8+8,60-16,50-16),50*16,80*16);
    painter.drawArc(QRectF(5+16,8+16,60-32,50-32),50*16,80*16);
    painter.drawArc(QRectF(5+24,8+24,60-48,50-40),50*16,80*16);
    #endif

    pen.setColor(Qt::white);
    //一级充电提示：时间1分钟后关机
    if( my_twinkleEnable )
    {
        if( my_twink )
        {
            pen.setColor(Qt::red);
            my_twink = false;
        }else{
            pen.setColor(Qt::gray);
            my_twink = true;
        }
    }


    //二级充电提示:%0开始提示,边框变红
    if( my_alarmEnable )
    {
          pen.setColor(Qt::red);
    }


    //充电时，电池框为黄色，内容递进式闪烁


    if( my_chargingEnable )
    {
       pen.setColor(Qt::yellow);
    }

    if( my_onlyPowerEnable )
    {
       pen.setColor(Qt::yellow);
    }



    pen.setWidth(3);
    painter.setPen(pen);
    painter.setBrush(QBrush());
    painter.drawRoundedRect(QRectF(70,8,48,25),5,5);
    pen.setWidth(4);
    pen.setCapStyle(Qt::RoundCap);
    painter.setPen(pen);
    painter.drawLine(120,14,120,27);



    //电量分十级 从红色到绿色
    int t_color_1;
    int t_color_2;


    if(  my_electProg-5 <= 0 )
    {
        t_color_1 = 0;
        t_color_2 = (my_electProg)*51;
        if( t_color_2 >255 ) t_color_2= 255;
    }else {
        t_color_1 = (my_electProg-5)*51;
        if( t_color_1 > 255) t_color_1= 255;
        t_color_2 = 255;
    }


    pen.setColor(QColor(255-t_color_1, t_color_2,0));
    painter.setBrush(QColor(255-t_color_1, t_color_2,0));


    pen.setWidth(1);
    painter.setPen(pen);
    if( !my_onlyPowerEnable )
    {
        for( int i = 0; i <= my_electProg; ++i)
        {
            painter.drawRect(73+(i)*4,12,3,18);
        }

        //qDebug() << my_chargingEnable << my_electProg << my_chargeCount << my_electProg+my_chargeCount << __FILE__<<__LINE__;

        if( my_chargingEnable )
        {
          my_chargeCount++;
          if( my_electProg+my_chargeCount > 10 )
          {
              my_chargeCount = 0;
          }

          for( int i = my_electProg; i <= my_electProg+my_chargeCount; ++i)
          {
              painter.drawRect(73+(i)*4,12,3,18);
          }

        }
    }else {
        for( int i = 0; i <= 10; ++i)
        {
            painter.drawRect(73+(i)*4,12,3,18);
        }
    }





    QRect elecLabel(42,8,100,26);

    pen.setColor(Qt::black);
    painter.setPen(pen);
    QRect timeLabel(115,0,100,40);
    QFont timeFont;
    timeFont.setPixelSize(13);
    timeFont.setWeight(80);
    painter.setFont(timeFont);
    painter.drawText(timeLabel,Qt::AlignCenter,this->dateTime.toString("yyyy-MM-dd\nhh:mm:ss"));
    if( !my_chargingEnable )
    {
        painter.drawText(elecLabel,Qt::AlignCenter,QString().number(my_electPerc) + "%");
    }


}


void StateBar::setBatprog(int prog)
{

}
